package root;

import com.dtflys.forest.utils.StringUtils;
import root.service.DaemonFunctionService;
import root.service.impl.DaemonFunctionServiceImpl;

import java.io.File;

public class MonitorDaemonApplication {
    public static void main(String[] args) {
        initEnv(args);
        var jarPath=System.getProperty("monitor-jar-path");
        DaemonFunctionService daemonFunctionService= DaemonFunctionServiceImpl.getInstance();
        while (true){
            try{
                Thread.sleep(10000);
                if (daemonFunctionService.terminate()) {
                    System.out.println("退出");
                    break;
                }
                if (!daemonFunctionService.isMonitorRunning()){
                    System.err.println("monitor未启动，正在准备启动");
                    if (new File(jarPath).exists()) daemonFunctionService.runMonitorJar(jarPath, System.out::println);
                }
                daemonFunctionService.ping();
                System.out.println("检查更新中。。。");
                var upgradeEntity=daemonFunctionService.checkUpgrade();
                if (upgradeEntity!=null) System.out.printf("最新版本：%d\n",upgradeEntity.getVersionCode());
                var currentVersionCode=daemonFunctionService.getMonitorVersionCode();
                System.out.printf("当前版本：%d\n",currentVersionCode);
                if (upgradeEntity!=null&&upgradeEntity.getVersionCode()>currentVersionCode){
                    if (upgradeEntity.getDownloadUrl()==null||!upgradeEntity.getDownloadUrl().endsWith(".jar")){
                        System.err.printf("下载链接错误：%s\n",upgradeEntity.getDownloadUrl());
                        continue;
                    }
                    System.out.printf("中止进程：%d\n",daemonFunctionService.getMonitorPID());
                    daemonFunctionService.killMonitorProcess();
                    System.out.println("删除文件："+System.getProperty("monitor-jar-path"));
                    daemonFunctionService.deleteMonitorJar();
                    System.out.println("正在升级");
                    var downloadResult=daemonFunctionService.downloadLastMonitorJar();
                    if (!downloadResult){
                        System.err.println("下载失败");
                        continue;
                    }
                    daemonFunctionService.runMonitorJar(System.getProperty("monitor-jar-path"),System.out::println);
                }
            }catch (Exception e){
                System.err.printf("while内报错：%s\n",e.getMessage());
                e.printStackTrace();
            }
        }
    }
    public static void initEnv(String[] args){
        var userDir = System.getProperty("user.home");
        var monitorDir=userDir+"/appdata/local/monitor";
        var monitorDirFile=new File(monitorDir);
        if (!monitorDirFile.exists()) {
            var mkResult=monitorDirFile.mkdirs();
            System.out.println("创建路径 "+monitorDirFile+" "+mkResult);
            System.setProperty("monitor-home",monitorDir);
        }
        System.setProperty("monitor-jar-path",monitorDir+"/monitor-main-1.0-SNAPSHOT-jar-with-dependencies.jar");
        if (args.length==0||StringUtils.isBlank(args[0]))
            System.setProperty("api","http://43.136.84.55:1282");
        else
            System.setProperty("api",args[0]);

        if (args.length<1||StringUtils.isBlank(args[1]))
            System.setProperty("java","java");
        else
            System.setProperty("java",args[1]);
    }
}
